﻿
Public Module urlhandlers

    Public Const defaultConfigfile As String = "urlhandlers.ini"
    Public WithEvents config As configMonitor = Nothing
    Public WithEvents server As processorHttpServer = Nothing

    Public Sub shutdown()
        If Not server Is Nothing Then
            server.stop()
        End If
    End Sub

    Public Sub unhandledException(ByVal ex As Exception)
        shutdown()
        finishmain()
    End Sub

    Public Sub startservice()
        server = New processorHttpServer()
        Dim port As Int32
        port = config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.port _
                                , configNaming.defaultValue.urlhandlers.port)
        server.addport(port)
        server.addRequestProcessor(New buildInfoPage())
        server.addRequestProcessor(New favicon())
        server.addRequestProcessor(New staticrequest())
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableReloader _
                            , configNaming.defaultValue.urlhandlers.enableReloader) Then
            server.addRequestProcessor(New reloader())
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableRedirector _
                             , configNaming.defaultValue.urlhandlers.enableRedirector) Then
            server.addRequestProcessor(New redirector())
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableSnapshot _
                             , configNaming.defaultValue.urlhandlers.enableSnapshot) Then
            server.addRequestProcessor(New snapshot())
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableCounter _
                             , configNaming.defaultValue.urlhandlers.enableCounter) Then
            server.addRequestProcessor(New counter(), config)
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableUrlShorten _
                             , configNaming.defaultValue.urlhandlers.enableUrlShorten) Then
            server.addRequestProcessor(New urlshorten(), config)
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableTimenow _
                             , configNaming.defaultValue.urlhandlers.enableTimenow) Then
            server.addRequestProcessor(New timenow2(), config)
            server.addRequestProcessor(New timenow(), config)
        End If
        If config.parameter(configNaming.urlhandlers.name, configNaming.urlhandlers.enableHomepage _
                             , configNaming.defaultValue.urlhandlers.enableHomepage) Then
            server.addRequestProcessor(New homepage())
        End If

        raiseError("server will start on port " + Convert.ToString(port))
        server.start()
    End Sub

    Public Sub main(ByVal args() As String)
        startmain(AddressOf unhandledException)
        Dim configfile As String = defaultConfigfile
        If args.Length() > 0 Then
            configfile = args(0)
        End If
        config = New configMonitor()
        assert(config.open(configfile), "cannot open configuration file " + configfile)

        startservice()

        While Not consoleExtSignal() AndAlso (server.health() OrElse config.refreshing())
            sleep()
        End While

        If consoleExtSignal() Then
            raiseError("caught console cancel key event, shutdown ...")
        ElseIf Not server.health() Then
            raiseError("caught httpServer error, shutdown ...", errorHandle.errorType.critical)
        Else
            assert(False, "caught unhandled exception, shutdown ...")
        End If

        shutdown()
        finishmain()
        forceQuit(0)
    End Sub

    Private Sub config_ConfigChanged() Handles config.ConfigChanged
        shutdown()
        startservice()
    End Sub

    Private Sub server_receive(ByVal context As httpContext) Handles server.receive
        assert(False, "should not get server.receive event in processorHttpServer.")
    End Sub
End Module
